function LDAPDataTreeView() {}

LDAPDataTreeView.prototype = {
	visibleData : [
		["dc=beretti,dc=org", true, false],
	],

	get rowCount() { return this.visibleData.length; },

	treeBox: null,
	selection: null,

	setTree: function(treeBox)         { this.treeBox = treeBox; },
	getCellText: function(idx, column) { return this.visibleData[idx][0]; },
	isContainer: function(idx)         { return this.visibleData[idx][1]; },
	isContainerOpen: function(idx)     { return this.visibleData[idx][2]; },
	isContainerEmpty: function(idx)    { return false; },
	isSeparator: function(idx)         { return false; },
	isSorted: function()               { return false; },
	isEditable: function(idx, column)  { return false; },

	getParentIndex: function(idx)
	{
		var item = this.visibleData[idx];

		/*jsdump("getParentIndex ( "+idx+" - "+item[0]+" )");*/
		for (var t = idx - 1; t >= 0 ; t--)
		{
			var curitem = this.visibleData[t];
			if (item[0].indexOf(curitem[0]) == item[0].length - curitem[0].length)
			{
				return t;
			}
		}
		return -1;
	},

	getLevel: function(idx)
	{
		var item = this.visibleData[idx];

		/*jsdump("getLevel ( "+idx+" - "+item[0]+" )");*/
		var level = 0;
		var tmpidx = this.getParentIndex(idx);
		while (tmpidx != -1)
		{
			tmpidx = this.getParentIndex(tmpidx);
			level++;
		}
		/*jsdump("getLevel returned: "+level);*/
		return level;
	},

	hasNextSibling: function(idx, after)
	{
		var thisLevel = this.getLevel(idx);
		for (var t = idx + 1; t < this.visibleData.length; t++)
		{
			var nextLevel = this.getLevel(t)
			if (nextLevel == thisLevel) return true;
			else if (nextLevel < thisLevel) return false;
		}
		return false;
	},

	toggleOpenState: function(idx)
	{
		var item = this.visibleData[idx];
		if (item[2])
		{
			item[2] = false;

			var thisLevel = this.getLevel(idx);
			var deletecount = 0;
			for (var t = idx + 1; t < this.visibleData.length; t++) {
				if (this.getLevel(t) > thisLevel) deletecount++;
				else break;
			}
			if (deletecount) {
				this.visibleData.splice(idx + 1, deletecount);
				this.treeBox.rowCountChanged(idx + 1, -deletecount);
			}
		}
		else
		{
			item[2] = true;
			var dn = this.visibleData[idx][0];
			var TVSearchListener = new TreeViewSearchLDAPMessageListener(this, idx);
			var ldapOp = Components.classes["@mozilla.org/network/ldap-operation;1"].createInstance().QueryInterface(Components.interfaces.nsILDAPOperation);
			var proxy = getProxyOnUIThread(TVSearchListener, Components.interfaces.nsILDAPMessageListener);
			ldapOp.init(gLdapConn, proxy, null);
			ldapOp.searchExt(dn, Components.interfaces.nsILDAPURL.SCOPE_ONELEVEL, "(objectClass=*)", 1, ["hasSubordinates"], 0, 0);
		}

		this.treeBox.invalidateRow(idx);
	},

	getImageSrc: function(idx, column) {},
	getProgressMode : function(idx,column) {},
	getCellValue: function(idx, column) {},
	cycleHeader: function(col, elem) {},
	selectionChanged: function() {},
	cycleCell: function(idx, column) {},
	performAction: function(action) {},
	performActionOnCell: function(action, index, column) {},
	getRowProperties: function(idx, column, prop) {},
	getCellProperties: function(idx, column, prop) {},
	getColumnProperties: function(column, element, prop) {},

	AddChildToNode: function(idx, dn, i)
	{
		jsdump("AddChildToNode ( "+idx+" - "+dn+" )");

		var item = this.visibleData[idx];
		if (!item[1]) return -1;

		this.visibleData.splice(idx + i + 1, 0, [dn, true, false]);
		this.treeBox.rowCountChanged(idx + 1, i+1);

		return idx + i + 1;
	},

	SetNodeHasNoChild: function(idx)
	{
		jsdump("SetNodeHasNoChild ( "+idx+" )");

		var item = this.visibleData[idx];

		item[1] = 0;

		this.treeBox.invalidateRow(idx);
	}
};

